package com.dream.inspect.modules.business.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.dream.inspect.common.utils.DateUtils;
import com.dream.inspect.common.utils.PageUtils;
import com.dream.inspect.common.utils.Query;
import com.dream.inspect.modules.business.dao.SuperviseTargetDao;
import com.dream.inspect.modules.business.entity.SuperviseTargetEntity;
import com.dream.inspect.modules.business.service.SuperviseTargetService;


@Service("superviseTargetService")
public class SuperviseTargetServiceImpl extends ServiceImpl<SuperviseTargetDao, SuperviseTargetEntity> implements SuperviseTargetService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<SuperviseTargetEntity> page = this.selectPage(
                new Query<SuperviseTargetEntity>(params).getPage(),
                new EntityWrapper<SuperviseTargetEntity>()
        );

        return new PageUtils(page);
    }

	@Override
	public List<SuperviseTargetEntity> findSuperviseTargetList(Map<String, String> params) {

		int currPage = Integer.valueOf(params.get("currPage"));
    	int pageSize  = Integer.valueOf(params.get("pageSize"));
    	currPage = (currPage - 1) * pageSize;
		List<SuperviseTargetEntity> stList = new ArrayList<SuperviseTargetEntity>();
		//拼接查询条件
		EntityWrapper<SuperviseTargetEntity> wrapper = addCondition(params);
		//查询数据
		stList = baseMapper.findSuperviseTargetList(wrapper, currPage, pageSize);
		return stList;
	}

	@Override
	public int findSuperviseTargetListCount(Map<String, String> params) {
		int countNum = 0;
		//拼接查询条件
		EntityWrapper<SuperviseTargetEntity> wrapper = addCondition(params);
		//查询总件数
		countNum = baseMapper.findSuperviseTargetListCount(wrapper);
		return countNum;
	}
	
	@Override
	public Long insertSuperviseTarget(Map<String, String> params) {
		
		Long result = 0L;
        Date now = new Date();
        String userId = params.get("userId");
        String projectId = params.get("projectId");
        String toSsdwId = params.get("toSsdwId");
        String ssTime = params.get("ssTime");
        String ssStatus = params.get("ssStatus");
        
        SuperviseTargetEntity stEntity = new SuperviseTargetEntity();
        if (StringUtils.isNotEmpty(projectId)) {
        	stEntity.setProjectId(Long.valueOf(projectId));                              //督察专项ID
        }
        if (StringUtils.isNotEmpty(toSsdwId)) {
        	stEntity.setToSsdwId(Long.valueOf(toSsdwId));                               //被实施单位ID
        }
        if (StringUtils.isNotEmpty(ssTime)) {
        	stEntity.setSsTime(DateUtils.stringToDate(ssTime, DateUtils.DATE_TIME_PATTERN));//实施时间
        }
        if (StringUtils.isNotEmpty(ssStatus)) {
        	stEntity.setSsStatus(ssStatus);                                             //实施状态(0：待实施，1：已执行)
        }
        
        stEntity.setCreateUser(Long.valueOf(userId));           //创建人id
        stEntity.setCreateTime(now);                            //创建时间
        stEntity.setUpdateUser(Long.valueOf(userId));           //更新人id
        stEntity.setUpdateTime(now);                            //更新时间
        
        //save
        this.insert(stEntity);
        
        result = stEntity.getId();
        return result;
	}
	
	@Override
	public Long insertSuperviseTarget(SuperviseTargetEntity stEntity) {
		Long result = 0L;
        Date now = new Date();
       
        stEntity.setCreateTime(now);                            //创建时间
        stEntity.setUpdateTime(now);                            //更新时间
        
        //save
        this.insert(stEntity);
        
        result = stEntity.getId();
        return result;
	}
	

	@Override
	public int deleteSuperviseTargetByCondition(Map<String, String> params) {
		int result = 0;
		SuperviseTargetEntity stEntity = new SuperviseTargetEntity();
		String projectId = params.get("id");
		if (StringUtils.isNotEmpty(projectId)) {
        	stEntity.setProjectId(Long.valueOf(projectId));                              //督察专项ID
        }
		result = baseMapper.deleteSuperviseTargetByCondition(stEntity);
		return result;
	}
	
	/**
	 * 拼接查询条件
	 * 
	 * @param params
	 * @return
	 */
	private EntityWrapper<SuperviseTargetEntity> addCondition(Map<String, String> params) {
		String projectId = params.get("projectId");
		
		EntityWrapper<SuperviseTargetEntity> wrapper = new EntityWrapper<SuperviseTargetEntity>();
		wrapper.setEntity(new SuperviseTargetEntity());
		//专项ID
		if (StringUtils.isNotEmpty(projectId)) {
			wrapper.eq("PROJECT_ID", projectId);
		}
    	return wrapper;
	}

}
